package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsAgentProfitStatisMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsAgentProfitStatisMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.AgentProfitStatis;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.enums.RoleTypeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.ProfitDetailInfoBean;
import cc.rengu.igas.bsps.facade.request.AgentMchntProfitDetailQueryRequest;
import cc.rengu.igas.bsps.facade.response.AgentMchntProfitDetailQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 代理商分润详情查询
 * Created by 王成 on 2020/3/24
 */
public class AgentMchntProfitDetailQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        AgentMchntProfitDetailQueryRequest agentMchntProfitDetailQueryRequest = new AgentMchntProfitDetailQueryRequest();
        ConvertUtil.convertOutput(agentMchntProfitDetailQueryRequest);
        return agentMchntProfitDetailQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        AgentMchntProfitDetailQueryRequest agentMchntProfitDetailQueryRequest = (AgentMchntProfitDetailQueryRequest) request;
        AgentMchntProfitDetailQueryResponse agentMchntProfitDetailQueryResponse = new AgentMchntProfitDetailQueryResponse();
        BeanUtil.beanCopy(agentMchntProfitDetailQueryRequest, agentMchntProfitDetailQueryResponse);
        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(agentMchntProfitDetailQueryRequest.getUserId()) || StringUtil.isEmptyOrNull(agentMchntProfitDetailQueryRequest.getAgentMchntNo()) || StringUtil.isEmptyOrNull(agentMchntProfitDetailQueryRequest.getSettleDate())) {
            rglog.error("用户编号<{}>,代理商编号<{}>,清算日期<{}>不能为空!", agentMchntProfitDetailQueryRequest.getUserId(), agentMchntProfitDetailQueryRequest.getAgentMchntNo(), agentMchntProfitDetailQueryRequest.getSettleDate());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 获取用户角色，判断查询用户是否为代理商 */
        BspsMchntBindInfoMapper bspsMchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        MchntBindInfo mchntBindInfo = bspsMchntBindInfoMapper.selectBspsMchntBindInfoListByUserIdAndMchntNo(agentMchntProfitDetailQueryRequest.getHeader().getInstId(), agentMchntProfitDetailQueryRequest.getUserId(), agentMchntProfitDetailQueryRequest.getAgentMchntNo());
        if (null == mchntBindInfo || (!mchntBindInfo.getBindUserRole().equals(RoleTypeEnum.AGENT_MCHNT.getRoleType()) && !mchntBindInfo.getBindUserRole().equals(RoleTypeEnum.PLAT_MCHNT.getRoleType()))) {
            rglog.error("非法用户，不允许当前查询操作,userId:<{}>,agentMchntNo:<{}>", agentMchntProfitDetailQueryRequest.getUserId(), agentMchntProfitDetailQueryRequest.getAgentMchntNo());
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }
        /* 查询代理商分润信息 */
        BspsAgentProfitStatisMapper bspsAgentProfitStatisMapper = new BspsAgentProfitStatisMapperImpl();

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(agentMchntProfitDetailQueryRequest.getPageIndex());
        pageInfo.setPageSize(agentMchntProfitDetailQueryRequest.getPageSize());
        PageResult pageResult = bspsAgentProfitStatisMapper.selectBspsAgentProfitStatisInfoBySettleDatePage(agentMchntProfitDetailQueryRequest.getHeader().getInstId(), agentMchntProfitDetailQueryRequest.getAgentMchntNo(), agentMchntProfitDetailQueryRequest.getSettleDate(), pageInfo);
        List<ProfitDetailInfoBean> profitDetailInfos = new ArrayList<>();
        if (pageResult != null) {
            for (Object object : pageResult.getResult()) {
                AgentProfitStatis agentProfitStatis = (AgentProfitStatis) object;
                convProfitDetailInfo(agentProfitStatis, profitDetailInfos);
            }
            agentMchntProfitDetailQueryResponse.setProfitDetailInfoList(profitDetailInfos);
            agentMchntProfitDetailQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            agentMchntProfitDetailQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
        } else {
            agentMchntProfitDetailQueryResponse.setTotalPage(0);
            agentMchntProfitDetailQueryResponse.setTotalRows(0);
        }

        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(agentMchntProfitDetailQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AgentMchntProfitDetailQueryResponse agentMchntProfitDetailQueryResponse = (AgentMchntProfitDetailQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(agentMchntProfitDetailQueryResponse);
    }

    /**
     * 代理商分润详情信息转换
     *
     * @param agentProfitStatis 代理商分润统计信息
     * @param profitDetailInfos 接口返回信息
     */
    private void convProfitDetailInfo(AgentProfitStatis agentProfitStatis, List<ProfitDetailInfoBean> profitDetailInfos) {
        ProfitDetailInfoBean profitDetailInfo = new ProfitDetailInfoBean();
        profitDetailInfo.setMchntNo(agentProfitStatis.getMchntNo());
        profitDetailInfo.setTransTotalAmt(agentProfitStatis.getTotalTransAmt());
        profitDetailInfo.setTransTotalNum(agentProfitStatis.getTotalTransNum());
        profitDetailInfo.setMchntFee(agentProfitStatis.getMchntFee());
        profitDetailInfo.setProfitTotalAmt(agentProfitStatis.getProfitFee());
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
        try {
            MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(agentProfitStatis.getInstId(), agentProfitStatis.getMchntNo());
            if (null != mchntBaseInfo) {
                profitDetailInfo.setMchntName(mchntBaseInfo.getMchntName());
                profitDetailInfo.setMchntType(mchntBaseInfo.getMchntType());
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("获取商户基本信息失败:{}", byteArrayOutputStream.toString());
        }
        profitDetailInfos.add(profitDetailInfo);
    }
}
